package com.laoli.lab37;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * CompletableFuture示例5-任务编排
 */
public class CompletableFutureDemo05 {

    public static void main(String[] args) {
        // 问：如何实现 "任务A -> 任务B -> 任务C" 编排顺序?
        CompletableFuture.supplyAsync(() -> {
                    System.out.printf("【%s】执行任务A\n", Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000); // 模拟耗时操作
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.printf("【%s】结束任务A：%s\n", Thread.currentThread().getName(), LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
                    return "任务A";  // 模拟返回结果
                })
                .thenApply(result -> {
                    System.out.printf("任务B获取上阶段结果：%s\n", result);
                    System.out.printf("【%s】执行任务B\n", Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000); // 模拟耗时操作
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.printf("【%s】结束任务B：%s\n", Thread.currentThread().getName(), LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
                    return "任务B";  // 模拟返回结果
                })
                .thenApply(result -> {
                    System.out.printf("任务C获取上阶段结果：%s\n", result);
                    System.out.printf("【%s】执行任务C\n", Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000); // 模拟耗时操作
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.printf("【%s】结束任务C：%s\n", Thread.currentThread().getName(), LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
                    return "任务C";  // 模拟返回结果
                });
        try {
            Thread.sleep(60 * 1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

